﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;

namespace automat_gpw
{
    public class CompanyDAL
    {
        private IList<ICompany> companies = null;
        public int counter = 0;
        /// <summary>
        /// Metoda wczytująca dane z pliku txt
        /// </summary>
        public void LoadData()
        {
            String companyName = String.Empty, tempLine = String.Empty;
            
            this.companies = new List<ICompany>();

            Parallel.ForEach(Directory.GetFiles(@"D:\Projekty\Sharepoint\automat-gpw\automat-gpw\Data"), fileName =>

            //foreach (String fileName in Directory.GetFiles(@"D:\Projekty\Sharepoint\automat-gpw\automat-gpw\Data"))
            {
                FileInfo fi = new FileInfo(fileName);
                companyName = fi.Name.Replace(".txt", String.Empty);

                IList<ICompanyValue> values = null;

                using (StreamReader reader = new StreamReader(fileName))
                {
                    values = new List<ICompanyValue>();
                    while (!reader.EndOfStream)
                    {
                        counter++;
                        tempLine = reader.ReadLine();
                        if (tempLine.ToLower().StartsWith("name")) continue;
                        values.Add(new CompanyValue(this.ParseLine(tempLine)));
                    }
                }

                this.companies.Add(new Company() { Values = values });
            }
            );
        }

        protected ICompanyValue ParseLine(String line)
        {
            
            String[] tempValue = line.Split(',');
            ICompanyValue value = new CompanyValue();
            try
            {
                value.Date = DateTime.ParseExact(tempValue[1], "yyyyMMdd", CultureInfo.InvariantCulture);
                value.PriceOpen = decimal.Parse(tempValue[2].Replace(".", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                value.PriceHighest = decimal.Parse(tempValue[3].Replace(".", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                value.PriceLowest = decimal.Parse(tempValue[4].Replace(".", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                value.PriceClose = decimal.Parse(tempValue[5].Replace(".", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator));
                if (tempValue[6].Contains('.'))
                {
                    tempValue[6] = tempValue[6].Replace(".", String.Empty);
                    value.Volume = Int32.Parse(tempValue[6]);
                }
                else
                {
                    value.Volume = Int32.Parse(tempValue[6]);
                }

            }
            catch(Exception ex)
            {
                int w = 0;
            }
            return value;
        }
    }
}
